using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class LevelPlayEntityViewCtrl : TEntityViewController
    {
        /// <summary>
        /// 资源加载器
        /// </summary>
        public ResLoader resloader;
        /// <summary>
        /// jump的配置
        /// </summary>
        private JumpConfigAsset _jumpConfigAsset;
        private LevelPlayRegionController regionCtrl => LevelPlayModule.Instance.levelPlay.regionCtrl;
        protected override TViewEntity playerAvatarEntity => regionCtrl.entityCtrl.GetMainCharacter();

        public JumpConfigAsset jumpConfig
        {
            get
            {
                if (_jumpConfigAsset == null)
                {
                    _jumpConfigAsset = resloader.LoadABAsset<JumpConfigAsset>(PathConstant.GetJumpConfigAsset("JumpConfigAsset"));
                }
                return _jumpConfigAsset;
            }
        }

        public LevelPlayEntityViewCtrl()
        {
            resloader = ResLoader.Create();

            MsgDispatcher.AddListener<int, int, List<int>>(MsgEventType.Level_ShowBubble, RecvShowBubble);
            MsgDispatcher.AddListener<List<int>, int>(MsgEventType.Level_NotifyPlayEntityAnimAction, PlayEntityAnimAction);
            MsgDispatcher.AddListener<int, int, int>(MsgEventType.Level_UpdateEntityAlert, RecvSyncEntityAlert);
        }

        public void RecvShowBubble(int entityId, int bubbleGroupId, List<int> validBubbleIds)
        {
            LevelPlayBaseEntity entity = null;
            if (entityId != 0 && !regionCtrl.entityCtrl.entitys.TryGetValue(entityId, out entity))
            {
                return;
            }
            TViewEntity bubbleEntity = entityId != 0 ? entity : regionCtrl.entityCtrl.GetMainCharacter();
            var view = bubbleEntity.GetComponent<EntityUIViewComponent>(ETComponentType.EntityUIView);
            StartEntityBubble(view, bubbleGroupId, validBubbleIds).Forget();
        }

        /// <summary>
        /// 通知播放动作
        /// </summary>
        /// <param name="entityIds"></param>
        /// <param name="actionId"></param>
        public void PlayEntityAnimAction(List<int> entityIds, int actionId)
        {
            foreach (var entityId in entityIds)
            {
                if (!regionCtrl.entityCtrl.entitys.TryGetValue(entityId, out var entity))
                {
                    return;
                }
                var animComponent = entity.GetAnimationComponent();
                animComponent?.StartAnimAction(actionId);
            }
        }

        /// <summary>
        /// 更新实体警戒值，仅开始，降为0和升到100时更新，其他时候自行计算
        /// </summary>
        /// <param name="entityId"></param>
        /// <param name="tagetEntityId"></param>
        /// <param name="alertValue"></param>
        private void RecvSyncEntityAlert(int entityId, int tagetEntityId, int alertValue)
        {
            if (regionCtrl.entityCtrl.entitys.TryGetValue(entityId, out var entity))
            {
                UpdateEntityAlertAwait(entity, tagetEntityId, alertValue).Forget();
            }
        }

        private async UniTaskVoid UpdateEntityAlertAwait(LevelPlayBaseEntity entity, int tagetEntityId, int alertValue)
        {
            //获取目标
            if (!regionCtrl.entityCtrl.heroEntities.TryGetValue(tagetEntityId, out LevelCharacterEntity heroEntity))
            {
                LogGame.LogWarning("更新怪物警戒值时找不到英雄实体，可能是因为刚进入迷宫时怪物就在玩家旁边");
                return;
            }
            //实体朝向
            var moveComponent = entity.GetMoveComponent();
            var animComponent = entity.GetAnimationComponent();
            if (moveComponent != null)
            {
                if (alertValue > 0 && alertValue != 100) //开始面向目标
                {
                    moveComponent.StartLookAtTarget(heroEntity.transform).Forget();
                    animComponent.ResetAnim();
                }
                else if (alertValue == 0 || alertValue == 100)
                {
                    moveComponent.StopLookAtEntity();
                    animComponent.PlayAlert();
                }
            }
            //UI
            var view = entity.GetViewComponent();
            AddAwaitView(view);
            bool isShowing = await view.ShowHeadAlertView(heroEntity, alertValue, Vector2.zero);
            if (!isShowing)
            {
                RemoveAwaitView(view);
            }
        }

        #region period
        protected override void OnModuleGameplayInit()
        {

        }

        protected override void OnSceneLoaded()
        {

        }

        protected override void OnDispose()
        {
            base.OnDispose();
            if (resloader != null)
            {
                resloader.Put2Pool();
            }

            MsgDispatcher.RemoveListener<int, int, List<int>>(MsgEventType.Level_ShowBubble, RecvShowBubble);
            MsgDispatcher.RemoveListener<List<int>, int>(MsgEventType.Level_NotifyPlayEntityAnimAction, PlayEntityAnimAction);
            MsgDispatcher.RemoveListener<int, int, int>(MsgEventType.Level_UpdateEntityAlert, RecvSyncEntityAlert);

        }
        #endregion
    }
}
